home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '91 / Hacks '91 / Primes / Primes.s
Encoding:
Text File  |  1991-06-18  |  2.5 KB  |  72 lines  |  [TEXT/MPS ]

  1. // A TMON Professional Prime Number Generator
  2. // By Jay Zipnick 6/18/91
  3. //
  4. // This is a TMON Professional script which generates prime numbers
  5. // This implementation divides prime candidates by the known primes
  6. // up to the square root of the candidate. If every division of the
  7. // candidate has a remainder the candidate is prime. To save the
  8. // "known prime" divisors this uses the 512 bytes at PlayMem, and
  9. // as a result this implementation is limited to testing integers
  10. // below the square of the 257th prime. That is it can generate prime
  11. // number up to 2,627,641.
  12. //
  13. // Changing the variable €LoggingOutput to a non empty file name
  14. // (e.g., "Primes.Out") will save output to a file with this name.
  15. // Note that the implementation below uses the printf dcmd when
  16. // logging the output to a disk file.
  17. //
  18. ////////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////// Output control
  22.  
  23. Vars €LoggingOutput = ""                // NIL or the name of the output file
  24.  
  25. If €LoggingOutput == ""                    // Define output function: screen only or to disk
  26.     Alias Output,"Open Number Ω"        // Display in Num window (screen only)
  27. Else
  28.     Printing /File = €LoggingOutput        // Set the output file
  29.     Alias Output," ∂
  30.         Print printf ∂"%d∂" Ω ∂
  31.         Open Number Ω"                    // Print to disk and show in Num window
  32. End
  33.  
  34.  
  35. //////// Array storage
  36.  
  37. Vars €primesArray = PlayMem                // Remember small primes in an array
  38. Vars €primesEnd = PlayMem+.512            // And define alias for setting array entry
  39. Vars €primesPtr = €primesArray
  40. Alias storePrime," ∂
  41.     If €primesPtr < €primesEnd               ∂
  42.         Fill €primesPtr..€primesPtr+1,Ω<<.16 ∂
  43.         Vars €primesPtr = €primesPtr+2       ∂
  44.     End"
  45.  
  46.  
  47. //////// Initial primes (non calculated)
  48.  
  49. Output 2                                // We do not calculate the first two primes
  50. Output 3
  51. storePrime 3                            // Save this in our prime divisor array
  52.  
  53.  
  54. //////// Prime number generator
  55.  
  56. Vars €test=5                            // Start testing for primes from here on
  57. While 1                                    // Go “forever”, or until command-period
  58.     Vars €testPtr = €primesArray        // Point to list of prime divisors
  59.     Vars €testDiv = €testPtr^.W            // Get first one
  60.     While (€testDiv*€testDiv <= €test)    // Divide by primes up to SquareRoot of €test
  61.         break if !(€test \\ €testDiv)    // Break if €testDiv is an even divisor
  62.         Vars €testPtr = €testPtr + 2    // Get next test divisor
  63.         Vars €testDiv = €testPtr^.W
  64.     End
  65.  
  66.     If €testDiv*€testDiv > €test        // Was €test a prime number?
  67.         Output €test                    // If so, output it
  68.         StorePrime €test                // Remember it
  69.     End
  70.     Vars €test = €test + 2                // Get next candidate to check (skip evens)
  71. End
  72.